/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.common.client.model;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONValue;

public class EditProductMetaModel extends ProductMetaModel
{	
	protected int numberOfPriceBreaksMax;
	protected boolean downloadEnabled;
	protected int downloadMaxDays;
	protected int downloadMaxCount;
	List<AbbreviatedProductModel> products;

	public EditProductMetaModel(JSONObject jsonObject)
	{
		super(jsonObject);
		products = new ArrayList<AbbreviatedProductModel>();
		numberOfPriceBreaksMax = convertToInteger(jsonObject,"number_of_price_breaks_max");
		downloadEnabled = convertToBoolean(jsonObject,"download_enabled");
		downloadMaxDays = convertToInteger(jsonObject,"download_maxdays");
		downloadMaxCount = convertToInteger(jsonObject,"download_maxcount");
		buildProducts(jsonObject);
	}

	protected void buildProducts(JSONObject jsonObject)
	{
		products.clear();
		try {
			JSONValue productValue = jsonObject.get("products");

			if (productValue != null)
			{
				JSONArray jsonArray;
				if ((jsonArray = productValue.isArray()) != null)
				{
					int sz = jsonArray.size();
					for (int i = 0; i < sz; i++)
					{
						JSONValue jsonValue = jsonArray.get(i);
						JSONObject jsonAObject;
						if ((jsonAObject = jsonValue.isObject()) != null)
						{
							AbbreviatedProductModel productModel = new AbbreviatedProductModel(jsonAObject);
							products.add(productModel);
						}
					}
				}
			}
		}
		catch (Exception e) {}
	}
	
	public int getNumberOfPriceBreaksMax() {
		return numberOfPriceBreaksMax;
	}
	
	public boolean getDownloadEnabled() {
		return downloadEnabled;
	}
	
	public int getDownloadMaxDays() {
		return downloadMaxDays;
	}
	
	public int getDownloadMaxCount() {
		return downloadMaxCount;
	}
	
	public List<AbbreviatedProductModel> getProducts() {
		return products;
	}
}
